package com.football;

import data.FootBallMemberConstants;
import model.FootBallMember;
import model.Position;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;


public class DispMemberFrame extends JDialog {
	//定义查询的表格控件
	private JTabbedPane queryPanel;

	//场上位置查询控件
	private JPanel positionQuery;

	//是否是国家队成查询控件
	private JPanel nationTitlequery;

	//姓名查询控件
	private JPanel nameQuery;

	//姓名查询 是否支持模糊查找
	private JCheckBox nameLikeChk;

	//按球队查找控件
	private JPanel clubQuery;

	//查询按钮
	private JButton queryBtn;

	//新增按钮
	private JButton addButton;

	//关闭按钮
	private JButton cancelBtn;

	//查询结果数量显示lable
	private JLabel queryNubmer;

	//文字描述lable
	private JLabel desLable;

	//显示panel
	private JPanel showPanel;
	//滚动条
	private JScrollPane jScrollPane1;

	//球队下拉列表
	private JComboBox clubcomBox;

	//是否是国家队成员列表框
	private JComboBox nationTitleBox;

	//场上位置下拉框
	private JComboBox postionBox;

	//球队名称控件
	private JLabel clubLable;

	//定义显示table
	private JTable jTable;

	//姓名查询文本域
	private JTextField nameTxt;

	//姓名控件
	private JLabel nameLable;

	//定义table表格
	private DefaultTableModel  nameTableModel;

	public DispMemberFrame() {
		initGUI();
	}

	private void initGUI() {
		try {
			this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("Show the players");
			{
				queryPanel = new JTabbedPane();
				getContentPane().add(queryPanel);
				queryPanel.setBounds(17, 7, 448, 89);

				//姓名查找panel
				nameQuery = new JPanel();
				queryPanel.addTab("search by name", null, nameQuery, null);
				nameQuery.setPreferredSize(new java.awt.Dimension(473, 349));
				nameQuery.setLayout(null);

				//定义姓名空间,并设置到姓名查询的标签页上
				nameLable = new JLabel();
				nameQuery.add(nameLable);

				nameLable.setText("Name");
				nameLable.setBounds(26, 19, 53, 17);


				//定义姓名查询框 设置到标签页上
				nameTxt = new JTextField();
				nameQuery.add(nameTxt);
				nameTxt.setBounds(75, 16, 208, 24);


				//设置查询按钮，点击查询
				nameLikeChk = new JCheckBox();
				nameQuery.add(nameLikeChk);
				nameLikeChk.setText("Vague Search");
				nameLikeChk.setBounds(319, 17, 113, 21);

				clubQuery = new JPanel();
				queryPanel.addTab("Search by team", null, clubQuery, null);
				clubQuery.setPreferredSize(new java.awt.Dimension(400, 46));
				clubQuery.setLayout(null);

				//定义球队名称显示label控件,并设置到查询标签页上
				clubLable = new JLabel();
				clubQuery.add(clubLable);
				clubLable.setText("Name of Club");
				clubLable.setBounds(22, 12, 61, 17);


				//定义一个球队的选择的下拉列表框,并设置在球队查询的标签页上
				clubcomBox = new JComboBox();
				clubcomBox.addItem("Shanghai Greenland ");
				clubcomBox.addItem("Shandong Luneng");
				clubcomBox.addItem("Guangzhou EverGrand");
				clubcomBox.addItem("Barcelona");
				clubcomBox.addItem("Valencia ");
				clubcomBox.addItem("Real Madrid");
				clubcomBox.addItem("Atlético de Madrid ");
				clubcomBox.addItem("Sevilla");
				clubcomBox.addItem("Villarreal");
				clubcomBox.addItem("Athletic Club");
				clubcomBox.addItem("Real Club Celta de Vigo");
				clubcomBox.addItem("Málaga ");
				clubcomBox.addItem("Baren FC");
		        clubcomBox.addItem("Dortmond");
		        clubcomBox.addItem("Bayer 04 Leverkusen");
		        clubcomBox.addItem("Mönchengladbach ");
		        clubcomBox.addItem("FC Schalke 04");
		        clubcomBox.addItem("FSV Mainz 05 ");
		        clubcomBox.addItem("Hertha BSC Berlin");
		        clubcomBox.addItem("VfL Wolfsburg");
		        clubcomBox.addItem("TSG 1899 Hoffenheim");
		        clubcomBox.addItem("SV Werder Bremen");
		        clubcomBox.addItem("Eintracht Frankfurt Fußball AG");
				clubcomBox.addItem("Manchester United");
				clubcomBox.addItem("Manchester City");
				clubcomBox.addItem("Lester City");
				clubcomBox.addItem("Livepool");
				clubcomBox.addItem("Chelsea");
				clubcomBox.addItem("Arsenal");
				clubcomBox.addItem("Tottenham Hotspur ");
				clubcomBox.addItem("AC Millan");
				clubcomBox.addItem("Inter Millan");
				clubcomBox.addItem("Juventus");
				clubcomBox.addItem("Torino Football Club ");
				clubcomBox.addItem(" A.S.Roma");
				clubcomBox.addItem("ACF Fiorentina");
				clubcomBox.addItem("Paris-Saint-Germain F.C.");
				clubcomBox.addItem("Montpellier Hérault Sport Club");
				clubcomBox.addItem("Lille O.S.C.");
				clubcomBox.addItem("Olympique de Marseille");
				clubcomBox.addItem("FC Porto");
				clubcomBox.addItem("Sporting Clube de Portugal");
				clubcomBox.addItem("The Celtic Football Club ");
				clubcomBox.addItem("SL Benfica");
				clubcomBox.addItem("Philips Sports Union ");
				clubcomBox.addItem("Ajax Amsterdam");
				clubcomBox.addItem("Feyenoord Rotterdam ");
				clubcomBox.addItem("Alkmaar AZ Stichting ");
				clubcomBox.addItem("Sporting Clube de Braga ");
				clubcomBox.addItem("Moscow CSKA ");
				clubcomBox.addItem("Spartak Moscow ");
				clubcomBox.addItem("FC Zenit ");
				
				clubQuery.add(clubcomBox);

				clubcomBox.setBounds(95, 8, 156, 24);
				clubcomBox.setEditable(true);

				//国家队标签
				nationTitlequery = new JPanel();
				queryPanel.addTab("National team member", null, nationTitlequery, null);
				nationTitlequery.setPreferredSize(new java.awt.Dimension(443, 59));
				nationTitlequery.setLayout(null);

				//国家队列表框
				nationTitleBox = new JComboBox();
				nationTitleBox.addItem("National team member");
				nationTitleBox.addItem("Not National team member");
				nationTitlequery.add(nationTitleBox);
				nationTitleBox.setBounds(95, 8, 156, 24);
				nationTitleBox.setEditable(true);
				//场上位置标签页
				positionQuery = new JPanel();
				queryPanel.addTab("Position on court", null, positionQuery, null);
				positionQuery.setPreferredSize(new java.awt.Dimension(306, 19));
				positionQuery.setLayout(null);

				//设置场上位置选择下拉列表
				postionBox = new JComboBox();
				for (Position position : Position.values()){
					//添加位置下拉列表框
					postionBox.addItem(position.toString());
				}
				postionBox.setBounds(95, 8, 156, 24);
				postionBox.setEditable(true);
				positionQuery.add(postionBox);


			}

			//显示球员列表的控件
			showPanel = new JPanel();
			getContentPane().add(showPanel);
			showPanel.setBounds(17, 108, 548, 407);


			
			nameTableModel =
					new DefaultTableModel(
							new String[][] { { "One", "Two" }, { "Three", "Four" } },
							new String[] {
									"Name",
									"Age",
									"Used foot",
									"Position",
									"Salary",
									"Team",
									"Height",
									"ability",
									"National team",
									"Modify"
							});

			//获取球员列表信息
			List<FootBallMember> members = FootBallMemberConstants.getFootBallMembers();
			nameTableModel.removeRow(0);
			nameTableModel.removeRow(0);
			
			
			for (FootBallMember footBallMember : members){
				//构造table数据
				String[] perRow = new String[10];
				perRow[0] = footBallMember.getName();
				perRow[1] = footBallMember.getAge()+"";
				perRow[2] = footBallMember.getMostFooterEnum().toString();
				perRow[3] = footBallMember.getPosition().toString();
				perRow[4] = footBallMember.getSalary().toPlainString();
				perRow[5] = footBallMember.getClub();
				perRow[6] = footBallMember.getHeight();
				perRow[7] = footBallMember.getAbilityValue()+"";
				perRow[8] = footBallMember.isNation()?"Yes":"No";
				perRow[9] = "Modify";

				//添一条table记录
				nameTableModel.addRow(perRow);
			}
			jTable = new JTable();
			
			jTable.setModel(nameTableModel);
			jTable.getColumn("Modify").setCellRenderer(new TableUpdateRender(this));
//			jTable.setPreferredSize(new java.awt.Dimension(440, 116));
			//定义滑动控件
			jScrollPane1 = new JScrollPane();
//			jScrollPane1.setPreferredSize(new java.awt.Dimension(443, 198));
//			jScrollPane1.getVerticalScrollBar().setPreferredSize(new java.awt.Dimension(15, 216));
			jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
			jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
			jScrollPane1.setViewportView(jTable);
			
			showPanel.add(jScrollPane1);


			//结果数量显示标签
			desLable = new JLabel();
			getContentPane().add(desLable);
			desLable.setText("Query results in a number of:");
			desLable.setBounds(24, 534, 139, 17);

			//数量标签
			queryNubmer = new JLabel();
			getContentPane().add(queryNubmer);
			queryNubmer.setText(members.size() + "");
			queryNubmer.setBounds(135, 534, 33, 17);

			//确定查询按钮
			queryBtn = new JButton();
			getContentPane().add(queryBtn);
			queryBtn.setText("Query");
			queryBtn.setBounds(245, 531, 73, 24);
			queryBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					//点击确认按钮执行的事件
					doOkVerifyMethod(evt);
				}
			});

			addButton = new JButton();
			getContentPane().add(addButton);
			addButton.setText("Add member");
			addButton.setBounds(338, 531, 73, 24);
			addButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					//新增按钮触发事件
					doAddMethod(e);
				}
			});

			//关闭按钮
			cancelBtn = new JButton();
			getContentPane().add(cancelBtn);
			cancelBtn.setText("Close");
			cancelBtn.setBounds(418, 531, 74, 24);
			cancelBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					cancelButton();
				}
			});

			this.setSize(600, 600);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 点击新增球员按钮触发事件
	 * @param event
	 */
	protected void doAddMethod(ActionEvent event){
		//关闭当前界面
		this.setVisible(false);
		this.dispose();
		//打开新增界面
		AddMemberFrame addMemberFrame = new AddMemberFrame();
		addMemberFrame.setVisible(true);

	}
	/**
	 * 点击查询按钮触发事件
	 * @param evt
	 */
	protected void doOkVerifyMethod(ActionEvent evt) {

		if (nameQuery.isShowing()){
			String theQueryName = nameTxt.getText();
			System.out.println("According to the name of the query parameters : name -" + theQueryName);
			//按姓名查询
			boolean isLikeSelected = nameLikeChk.isSelected();
			List<FootBallMember> queryMembers = new ArrayList<FootBallMember>();
			List<FootBallMember> members = FootBallMemberConstants.getFootBallMembers();
			if (isLikeSelected){
				//模糊查询选中

				for (FootBallMember footBallMember : members){
					String name = footBallMember.getName();
					if (name.contains(theQueryName)){
						//符合查询条件加入到结果列表
						queryMembers.add(footBallMember);
					}
				}
			}else {
				//全词匹配
				for (FootBallMember footBallMember:members){
					String currentName = footBallMember.getName();
					if (currentName.equals(theQueryName)){
						//加入到查询结果中
						queryMembers.add(footBallMember);
					}
				}
			}

			//先清空界面显示的表格，再赋值
			while (nameTableModel.getRowCount() > 0){
				//清空第一行
				nameTableModel.removeRow(nameTableModel.getRowCount() - 1);
			}
			//赋值
			for (FootBallMember footBallMember : queryMembers){
				//构造table数据
				String[] perRow = new String[9];
				perRow[0] = footBallMember.getName();
				perRow[1] = footBallMember.getAge()+"";
				perRow[2] = footBallMember.getMostFooterEnum().toString();
				perRow[3] = footBallMember.getPosition().toString();
				perRow[4] = footBallMember.getSalary().toPlainString();
				perRow[5] = footBallMember.getClub();
				perRow[6] = footBallMember.getHeight();
				perRow[7] = footBallMember.getAbilityValue()+"";
				perRow[8] = footBallMember.isNation()?"YES":"NO";
				//添一条table记录
				nameTableModel.addRow(perRow);
			}

			queryNubmer.setText(queryMembers.size() + "");


		}
		//按球队查找
		if (clubQuery.isShowing()){
			//选中的球队
			String clubQueryString = (String) clubcomBox.getSelectedItem();
			List<FootBallMember> members = FootBallMemberConstants.getFootBallMembers();

			//根据球队的查询结果
			List<FootBallMember> clubQueryResult = new ArrayList<FootBallMember>();

			for (FootBallMember footBallMember : members){
				String currentClub = footBallMember.getClub();
				if (currentClub.equals(clubQueryString)){
					//和所要查询的球队相符则加入结果集
					clubQueryResult.add(footBallMember);
				}
			}
			//构造结果集，删除

			while (nameTableModel.getRowCount() > 0){
				nameTableModel.removeRow(nameTableModel.getRowCount() - 1);
			}
			//赋值
			for (FootBallMember footBallMember : clubQueryResult){
				//构造table数据
				String[] perRow = new String[9];
				perRow[0] = footBallMember.getName();
				perRow[1] = footBallMember.getAge()+"";
				perRow[2] = footBallMember.getMostFooterEnum().toString();
				perRow[3] = footBallMember.getPosition().toString();
				perRow[4] = footBallMember.getSalary().toPlainString();
				perRow[5] = footBallMember.getClub();
				perRow[6] = footBallMember.getHeight();
				perRow[7] = footBallMember.getAbilityValue()+"";
				perRow[8] = footBallMember.isNation()?"YES":"NO";
				//添一条table记录
				nameTableModel.addRow(perRow);
			}
			queryNubmer.setText(clubQueryResult.size() + "");
		}
		//根据国家队查询
		if (nationTitlequery.isShowing()){
			List<FootBallMember> members = FootBallMemberConstants.getFootBallMembers();
			List<FootBallMember> result = new ArrayList<FootBallMember>();
			//获取国家队的查询条件
			String nationTitle = (String) nationTitleBox.getSelectedItem();
			boolean queryBoolean = true;
			if (nationTitle.equals("Nation Team Member ")){
				queryBoolean = true;

			}else {
				queryBoolean = false;
			}
			for (FootBallMember footBallMember : members){
				//当前值
				boolean isNation = footBallMember.isNation();
				if (isNation == queryBoolean){
					//加入结果集
					result.add(footBallMember);
				}

			}
			//先清空
			while (nameTableModel.getRowCount() > 0){
				nameTableModel.removeRow(nameTableModel.getRowCount() - 1);

			}
			//赋值
			for (FootBallMember footBallMember : result){
				//构造table数据
				String[] perRow = new String[9];
				perRow[0] = footBallMember.getName();
				perRow[1] = footBallMember.getAge()+"";
				perRow[2] = footBallMember.getMostFooterEnum().toString();
				perRow[3] = footBallMember.getPosition().toString();
				perRow[4] = footBallMember.getSalary().toPlainString();
				perRow[5] = footBallMember.getClub();
				perRow[6] = footBallMember.getHeight();
				perRow[7] = footBallMember.getAbilityValue()+"";
				perRow[8] = footBallMember.isNation()?"Yes":"No";
				//添一条table记录
				nameTableModel.addRow(perRow);
			}
			queryNubmer.setText(result.size() + "");



		}
		//根据场上位置查询
		if (positionQuery.isShowing()){
			List<FootBallMember> members = FootBallMemberConstants.getFootBallMembers();
			//查询结果
			List<FootBallMember> result = new ArrayList<FootBallMember>();
			//获取查询条件
			String queryString = (String) postionBox.getSelectedItem();
			Position queryPostion = null;
			for (Position position : Position.values()){
				if (position.toString().equals(queryString)){
					queryPostion = position;
				}
			}
			int size = members.size();
			System.out.println(size);
			for (int i =0 ; i < size ; i++){
				Position currentPostion = members.get(i).getPosition();
				if (currentPostion.toString().equals(queryPostion.toString())){

					result.add(members.get(i));

				}

			}
			//清空table显示结果
			while (nameTableModel.getRowCount() > 0){
				nameTableModel.removeRow(nameTableModel.getRowCount() - 1);

			}
			//构造显示结果
			for (FootBallMember footBallMember : result){
				//构造table数据
				String[] perRow = new String[9];
				perRow[0] = footBallMember.getName();
				perRow[1] = footBallMember.getAge()+"";
				perRow[2] = footBallMember.getMostFooterEnum().toString();
				perRow[3] = footBallMember.getPosition().toString();
				perRow[4] = footBallMember.getSalary().toPlainString();
				perRow[5] = footBallMember.getClub();
				perRow[6] = footBallMember.getHeight();
				perRow[7] = footBallMember.getAbilityValue()+"";
				perRow[8] = footBallMember.isNation()?"Yes":"No";
				//添一条table记录
				nameTableModel.addRow(perRow);
			}
			queryNubmer.setText(result.size() + "");
		}
	}

	/**
	 * 关闭按钮
	 */
	protected void cancelButton() {
		//隐藏
		this.setVisible(false);

		//关闭
		this.dispose();

	}


}

